package com.hup.minicontroller.common.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * https://blog.csdn.net/chenweijiSun/article/details/52778965
 */
public abstract class BaseAdapter<E, T extends BaseViewHolder<E>> extends android.widget.BaseAdapter {

    protected ArrayList<E> data = new ArrayList<>();

    @Override
    public int getCount() {
        return data.size();
    }

    public E getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    public View getView(int position, View convertView, ViewGroup parent) {
        T holder;
        E item = this.getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(getLayoutResId(), parent, false);
            holder = createHolder();
            holder.bindView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (T) convertView.getTag();
        }
        holder.bindData(position, item);
        return convertView;
    }

    public abstract int getLayoutResId();

    public abstract T createHolder();

    public List<E> getData() {
        return this.data;
    }

    public void addNewData(List<E> data) {
        if (data != null) {
            this.data.addAll(0, data);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(Collection<? extends E> data) {
        if (data != null) {
            this.data.addAll(this.data.size(), data);
            this.notifyDataSetChanged();
        }
    }

    public void setData(Collection<? extends E> data) {
        this.data.clear();
        if (data != null) {
            this.data.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.data.clear();
        this.notifyDataSetChanged();
    }

    public void removeItem(int position) {
        this.data.remove(position);
        this.notifyDataSetChanged();
    }

    public void removeItem(E model) {
        this.data.remove(model);
        this.notifyDataSetChanged();
    }

    public void addItem(int position, E model) {
        this.data.add(position, model);
        this.notifyDataSetChanged();
    }

    public void addFirstItem(E model) {
        this.addItem(0, model);
    }

    public void addLastItem(E model) {
        this.addItem(this.data.size(), model);
    }

    public void setItem(int location, E newModel) {
        this.data.set(location, newModel);
        this.notifyDataSetChanged();
    }

    public void setItem(E oldModel, E newModel) {
        this.setItem(this.data.indexOf(oldModel), newModel);
    }

    public void moveItem(int fromPosition, int toPosition) {
        Collections.swap(this.data, fromPosition, toPosition);
        this.notifyDataSetChanged();
    }

}